package com.cl.common.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.cl.common.util.StringUtil;
import com.cl.core.orm.Page;
import com.cl.core.web.struts2.Struts2Utils;

public class PageBarFront extends BodyTagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6465683101573455492L;
	private String id;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int doEndTag() throws JspException {
		if ((this.bodyContent == null)
				|| (StringUtil.isEmpty(this.bodyContent.getString()))) {
			try {
				this.pageContext.getOut().print(getHtml());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return 6;
	}

	public String getHtml() {
		
		Page page = (Page) pageContext.getRequest().getAttribute("page");
		int pageNum=page.getPageNo();
		int pageSize=new Long(page.getTotalPages()).intValue();
		StringBuffer content = new StringBuffer();
		content.append("<table width='100%' cellpadding='1' cellspacing='0' border='0' style='table-layout: fixed;'>");
		content.append("<tr >" + "<td colspan='10' align='left'>" + "<input type=\"hidden\" name=\"page.orderBy\" id=\"orderBy\" value=\"" + page.getOrderBy() + "\" />" + "<input type='hidden' name='page.order' id='order' value=\"" + page.getOrder() + "\" />" + "<input type='hidden' name='page.pageNo' id='pageNo' value=\"" + page.getPageNo() + "\" />");
		if(pageNum!=1 && pageNum !=0){
			content.append("<div style='float: right; font-family: Tahoma'>" + "<a href='javascript:jumpPage(1)'>&laquo;第一页</a>" + "&nbsp;|&nbsp;" + "<s:if test='page.hasPre'>" + "<a href=\"javascript:jumpPage(" + page.getPrePage() + ")\">" + "上一页</a>&nbsp;|&nbsp;" + "</s:if>");
		}else{
			content.append("<div style='float: right; font-family: Tahoma'>" + "«第一页" + "&nbsp;|&nbsp;" + "<s:if test='page.hasPre'>" + "" + "上一页&nbsp;|&nbsp;" + "</s:if>");
		}
		
		if(pageNum!=pageSize){
		
		content.append("" + "<s:if test='page.hasNext'>" + "<a href=\"javascript:jumpPage(" + page.getNextPage() + ")\">下一页</a>" + "&nbsp;|&nbsp;" + "</s:if><a href='javascript:jumpPage(" + page.getTotalPages() + ")'>最末页»</a>" + "&nbsp;&nbsp;&nbsp;&nbsp;转到第&nbsp;");
		}else{
			content.append("" + "<s:if test='page.hasNext'>" + "下一页" + "&nbsp;|&nbsp;" + "</s:if>最末页»" + "&nbsp;&nbsp;&nbsp;&nbsp;转到第&nbsp;");
		}
		
		content.append("<select style='width:40px' id='")
			.append(id + "_textField").append("'>");
		for (int i = 1; i <= pageSize; i++) {
			content.append("<option value='").append(i).append("'")
					.append(i == pageNum ? "selected" : "").append(">")
					.append(i).append("</option>");
		}
		
		content.append("</select>");

		content.append("页");
		if(pageNum ==0 ){
			content.append("&nbsp;&nbsp;" +"跳转"+"</div>" + "<div style='float: left; font-family: Tahoma'>共 " + page.getTotalCount() + " 条记录，每页 " + page.getPageSize() + " 条，当前第 " + page.getPageNo() + "/" + page.getTotalPages() + " 页</div></td></tr></table>");
		}else{
			content.append("<input type='button' class='inputButton' value='跳转' onclick=\"goToPage('"+id+"');\"/>" + "</div>" + "<div style='float: left; font-family: Tahoma'>共 " + page.getTotalCount() + " 条记录，每页 " + page.getPageSize() + " 条，当前第 " + page.getPageNo() + "/" + page.getTotalPages() + " 页</div></td></tr></table>");
		}
		return content.toString();
		
	}

}
